/*
Copyright (C) Vincenzo Tilotta 2012 <tailot@gmail.com>

mygame.js is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option) any later version.

mygame.js is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
function ThePills(idcanvas){
	this.widthspace = $(document).width();
	this.heightspace = $(document).height();
	this.canvas = '';
	this.monster = '';
	this.pills = [];
	this.score = 0;
	this.lives = 0;
	this.audiochannel = '';
	this.cookie = '';
	
	this.init = function(){
		//Ripristina ambiente ambiente
		$('#start').remove();
		$('#background').css("display","block");
		//--------------
		//Init variabili globali
		this.lives = 3;
		this.audiochannel = new AudioPlay();
		this.cookie = new Cookie();
		this.cookie.setCookie('score','0');
		//--------------
		
		//Imposto il canvas dello schermo
		this.canvas = $('#'+idcanvas);
		this.canvas.css("width",this.widthspace);
		this.canvas.css("height",this.heightspace);
		this.canvas.css("position","relative");
		this.canvas.css("overflow","hidden");
		
		//--------------
		
		//Init della barra
		$("#bar").css("display","block");
		$('#lives').html('Lives '+this.lives);
		$('#score').html('Score '+0);
		//--------------
		
		//Impostazione dello sfondo
		$('#background').attr("width",this.widthspace);
		$('#background').attr("height",this.heightspace);
		this.setbackground();
		//--------------
		
		//Creazione del personaggio
		this.monster = new Sprite("monster",[100,333],"image/monster_off.png",idcanvas);
		this.monster.enablemove();
		//--------------

		//Idle vari
		this.idle_pills();
		this.idle_virus();
		this.idle_collision_pills();
		this.idle_collision_virus();
		//--------------
	};
	
	this.makepill = function(){
			var typepill = Math.floor((Math.random()*2));
			
			var mypill = '';
			if(typepill){
				var numberpills = Math.floor((Math.random()*6)+1);
				mypill = new Sprite("pill",[0,0],"image/pills/circlepill"+numberpills+".png",$(this.canvas).attr('id'));
			}else{
				var numberpills = Math.floor((Math.random()*7)+1);
				mypill = new Sprite("pill",[0,0],"image/pills/classicpill"+numberpills+".png",$(this.canvas).attr('id'));
			}
			mypill.enablerotate(0,500);
			
			var widthpill = parseInt($('#'+mypill.id).css('width').replace("px",""));
			var widthcanvas = parseInt(this.canvas.css("width").replace("px",""));
			var randomposition = Math.floor((Math.random()*2));
			var randompositionwidth = Math.floor((Math.random()*(widthcanvas- widthpill))+1);
			
			//decido casualmente se la pillola nasce dal basso o dall'alto
			if(randomposition){
				mypill.movenoanimation(randompositionwidth, 0);
				mypill.enablefall();
			}else{
				mypill.movenoanimation(randompositionwidth, this.canvas.css("height"));
				mypill.enablerises();
			}
			
			
	};
	
	this.makevirus = function() {
		var numbervirus = Math.floor((Math.random()*4)+1);
		var virus = new Sprite("virus",[0,0],"image/virus/"+numbervirus+".png",$(this.canvas).attr('id'));
		virus.enablerotate(0, 500);		
		
		var widthcanvas = parseInt(this.canvas.css("width").replace("px",""));
		var widthvirus = parseInt($('#'+virus.id).css('width').replace("px",""));
		var randompositionwidth = Math.floor((Math.random()*(widthcanvas- widthvirus))+1);
		var randomposition = Math.floor((Math.random()*2));
		if(randomposition){
			virus.movenoanimation(randompositionwidth, 0);
			virus.enablefall();			
		}else{
			virus.movenoanimation(randompositionwidth, this.canvas.css("height"));
			virus.enablerises();			
		}
	};
	this.setbackground = function(){
		var randombackground = Math.floor((Math.random()*8)+1);
		$('#background').attr("src","image/background/"+randombackground+".jpg");
	};
	
	this.gameover = function(){
		this.cookie.setCookie("score",this.score);
		window.location.href = 'score.html';
	};
	
	/*
	 * Crea pillole in loop
	 */
	this.idle_pills = function(){
		if($("#start").css("display") != "block"){
			this.makepill();
			setTimeout($.proxy(function(){ this.idle_pills(); }, this),3000);
		}
	};
	/*
	 * Crea virus in loop
	 */
	this.idle_virus = function(){
		if($("#start").css("display") != "block"){
			this.makevirus();
			setTimeout($.proxy(function(){ this.idle_virus(); }, this),1200);
		}
	};
	/*
	 * Intercetta le collisioni di monster e rimuove le pillole mangiate
	 */
	this.idle_collision_pills = function(){
		if($("#start").css("display") != "block"){
			var listcollision = $('#'+this.monster.id).collision('.pill');
			for(var i = 0; i < listcollision.length; i++){
				this.audiochannel.setAudio("/android_asset/www/sound/skill.mp3");
				var randomparticle = Math.floor((Math.random()*4)+1);
				particleStart(parseInt($(listcollision[i]).css("left").replace("px","")),parseInt($(listcollision[i]).css("top").replace("px","")),"image/particle/"+randomparticle+".png");
				$(listcollision[i]).remove();
				this.score = this.score + 100;
				$("#score").html("Score: "+this.score);
			}
			setTimeout($.proxy(function(){ this.idle_collision_pills(); }, this),50);
		}
	};
	this.idle_collision_virus = function(){
		if($("#start").css("display") != "block"){
			var listcollision = $('#'+this.monster.id).collision('.virus');
			if(listcollision.length > 0){
				if(this.lives <= 0){
					this.gameover();
					return;
				}
				//Collisione con il virus
				this.audiochannel.setAudio("/android_asset/www/sound/monster.ogg");
				particleStart(parseInt($(listcollision[0]).css("left").replace("px","")),parseInt($(listcollision[0]).css("top").replace("px","")),"image/skull.png");
				$(listcollision[0]).remove();
				this.lives = this.lives - 1;
				$('#lives').html('Lives '+this.lives);
			}
			setTimeout($.proxy(function(){ this.idle_collision_virus(); }, this),50);
		}
	};
	
	this.init();
};